import store from '@/store/index.js'
export default {
	appVersion: 'v1.0.0', // 版本号
	// 全局配置
	common: {
		baseUrl: store.state.debug.getWay, //后台地址
		header: {
			'Content-Type': 'application/json;charset=UTF-8',
			'Content-Type': 'application/x-www-form-urlencoded'
		},
		data: {},
		method: 'GET',
		dataType: 'json'
	},
	// 请求 返回promise
	request(options = {}) {
		this.common.baseUrl = store.state.debug.getWay;
		var apiUrl=options.url
		// 组织参数
		if (!options.is_url) {
			options.url = this.common.baseUrl + options.url //http://xxxx/xxx//xx
		}
		options.header = options.header || this.common.header //
		options.data = options.data || this.common.data
		options.method = options.method || this.common.method
		options.dataType = options.dataType || this.common.dataType
		// 是否传入token
		if (options.token) {
			if (uni.getStorageSync('token')) {
				options.data.token = uni.getStorageSync('token');
			} else {
				uni.removeStorageSync('token') // 清除登录token
				uni.showModal({
					title: '未登录',
					content: '请先登录',
					success: function(res) {
						if(res.confirm){
							uni.navigateTo({
								url: "/pages/login"
							});
						}else{
							uni.hideLoading()
						}
					}
				});
				return;
			}
		}
		
		// 请求
		return new Promise((res, rej) => {
			// 请求之前... todo
			// 请求中...
			uni.request({
				...options,
				success: (result) => {
					// console.log("success", result)
					//接口返回失败
					if (result.statusCode != 200) {
						if (result.statusCode == 401) { //没有权限
							uni.removeStorageSync('token') // 清除登录token
							uni.showModal({
								title: '未登录',
								content: '请先登录',
								success: function(res) {
									if(res.confirm){
										uni.navigateTo({
											url: "/pages/login"
										});
									}else{
										uni.hideLoading()
									}
								}
							});
							return res(false)
						}
						setTimeout(function(){
							uni.showToast({
								title: result.data.message || result.data.msg || '请求错误请联系管理员',
								icon: 'none'
							});
						},2000)
						return res(false)
					}
					//接口返回正常，但是接口逻辑失败
					if (result.statusCode == 200) {
						if (result.data.status == 0 || result.data.code == 0 || result.data.code ==-1) {
							let msg = result.data.message || result.data.msg || '请求错误请联系管理员';
							console.log(apiUrl)
							if(apiUrl=='order/confirm_order'||apiUrl=='user/new_user_coupon'||apiUrl=='verification/order'||apiUrl=='verification/tuangou_hexiao'||apiUrl=='douyin/tuangou_hexiao'){
								
							}else{
								uni.showToast({
									title: msg,
									icon: 'none'
								});
							}
							return res(result.data)
						}
						if (result.data.code == 401) {
							uni.removeStorageSync('token') // 清除登录token
							uni.showModal({
								title: '未登录',
								content: '请先登录',
								success: function(res) {
									if(res.confirm){
										uni.navigateTo({
											url: "/pages/login"
										});
									}else{
										uni.hideLoading()
									}
								}
							});
						}
					}
					// 成功
					let data = result.data;
					res(data)
				},
				fail: (error) => {
					console.log('fail',error)
					uni.showToast({
						title: error.errMsg || '请求失败',
						icon: 'none'
					});
					return rej()
				}
			});
		})
	},
	// get请求
	get(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	// post请求
	post(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	del(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	}
}